package com.cysnake.easyyi.Dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.cysnake.easyyi.tools.MySQLiteOpenHelper;
import com.cysnake.easyyi.vo.GuaVO;

public class GuaciDao extends BaseDao {
	public static final String TABLE_NAME = "guaci";
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_NAME_DEC = "namedec";
	public static final String KEY_TITLE = "title";
	public static final String KEY_TITLE_DEC = "titledec";
	public static final String KEY_FIRST = "first";
	public static final String KEY_FIRST_DEC = "firstdec";
	public static final String KEY_SECOND = "second";
	public static final String KEY_SECOND_DEC = "seconddec";
	public static final String KEY_THIRD = "third";
	public static final String KEY_THIRD_DEC = "thirddec";
	public static final String KEY_FOURTH = "fourth";
	public static final String KEY_FOURTH_DEC = "fourthdec";
	public static final String KEY_FIFTH = "fifth";
	public static final String KEY_FIFTH_DEC = "fifthdec";
	public static final String KEY_LAST = "last";
	public static final String KEY_LAST_DEC = "lastdec";
	public static final String KEY_CONMMENT = "comment";
	public static final String[] ALL_COLUMN = new String[] { KEY_ID, KEY_NAME,
			KEY_NAME_DEC, KEY_TITLE, KEY_TITLE_DEC, KEY_FIRST, KEY_FIRST_DEC,
			KEY_SECOND, KEY_SECOND_DEC, KEY_THIRD, KEY_THIRD_DEC, KEY_FOURTH,
			KEY_FOURTH_DEC, KEY_FIFTH, KEY_FIFTH_DEC, KEY_LAST, KEY_LAST_DEC,
			KEY_CONMMENT };

	private static final String TAG = "GuaciDao";
	private MySQLiteOpenHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;

	public GuaciDao(Context ctx) {
		this.mCtx = ctx;
	}

	public GuaciDao open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public GuaVO getById(int id) {
		Cursor mCursor = mDb.query(true, TABLE_NAME, ALL_COLUMN, KEY_ID + " = "
				+ id, null, null, null, null, null);
		List<GuaVO> list = changeCursor(mCursor);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public GuaVO getGuaByName(String name) {
		Cursor mCursor = mDb.query(true, TABLE_NAME, ALL_COLUMN, KEY_NAME
				+ " = '" + name + "'", null, null, null, null, null);
		List<GuaVO> list = changeCursor(mCursor);

		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public ArrayList<String> getAllName() {
		Cursor mCursor = mDb.query(true, TABLE_NAME, new String[] { KEY_NAME },
				null, null, null, null, KEY_ID, null);
		int temp;
		ArrayList<String> list = new ArrayList<String>();
		while (mCursor.moveToNext()) {
			temp = mCursor.getColumnIndex(KEY_NAME);
			list.add(mCursor.getString(temp));
		}
		mCursor.close();
		return list;
	}

	public String findNameByType(String dec) {
		Cursor mCursor = mDb.query(true, TABLE_NAME, new String[] { KEY_NAME,
				KEY_NAME_DEC }, KEY_NAME_DEC + " = '" + dec + "'", null, null,
				null, null, null);
		if (mCursor.moveToNext()) {
			int temp = mCursor.getColumnIndex(KEY_NAME);
			String result = mCursor.getString(temp);
			mCursor.close();
			return result;
		} else {
			mCursor.close();
			return null;
		}
	}

	private ArrayList<GuaVO> changeCursor(Cursor cursor) {
		ArrayList<GuaVO> list = new ArrayList<GuaVO>();
		while (cursor.moveToNext()) {
			GuaVO gua = new GuaVO();

			int temp = cursor.getColumnIndex(KEY_ID);
			gua.setId(cursor.getInt(temp));
			temp = cursor.getColumnIndex(KEY_NAME);
			gua.setName(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_NAME_DEC);
			gua.setNamedec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_TITLE);
			gua.setTitle(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_TITLE_DEC);
			gua.setTitledec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FIRST);
			gua.setFirst(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FIRST_DEC);
			gua.setFirstdec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_SECOND);
			gua.setSecond(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_SECOND_DEC);
			gua.setSeconddec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_THIRD);
			gua.setThird(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_THIRD_DEC);
			gua.setThirddec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FOURTH);
			gua.setFourth(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FOURTH_DEC);
			gua.setFourthdec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FIFTH);
			gua.setFifth(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_FIFTH_DEC);
			gua.setFifthdec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_LAST);
			gua.setLast(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_LAST_DEC);
			gua.setLastdec(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_CONMMENT);
			gua.setComment(cursor.getString(temp));
			list.add(gua);
		}
		cursor.close();
		return list;
	}

	public String getGuaNameDec(String name) {
		Cursor mCursor = mDb.query(true, TABLE_NAME,
				new String[] { KEY_NAME_DEC }, KEY_NAME + " = '" + name + "'",
				null, null, null, null, null);
		if (mCursor.moveToNext()) {
			int temp = mCursor.getColumnIndex(KEY_NAME_DEC);
			String result = mCursor.getString(temp);
			mCursor.close();
			return result;
		} else {
			mCursor.close();
			return null;
		}
	}
}
